<?php
/**
 * STEP 4
 */


include_once('../lib/config.php');
include_once('../lib/Db.class.php');
include_once('../lib/Model.class.php');
include_once('../lib/function.php');

$redis = new Redis();  
$ret = $redis->connect("localhost","6379");  //php客户端设置的ip及端口
if(!$ret) die('redis connction faild');
$redis->auth(REDIS_PASS);
$redis->select(10);

$lsize = $redis->lSize('QISUU_POST_LINK');
echo2($lsize . ' in queue', 'QISUU_POST');

/**
if($lsize < 100) {
    $total = 0;
    $files = scandir('./data/');
    foreach ($files as $file) {
        if(!stristr($file, '.html')) {
            continue;
        }

        $contents = file_get_contents('./data/'.$file);
        $contents = json_decode($contents, true);

        //检查是否已下载
        $url = $contents['download'][0];
        $basename = basename($url);
        $basename_arr = explode('.', $basename);
        $ext = array_pop($basename_arr);
        if(!empty($basename) && (empty($ext) || $ext == 'ar' || ($basename == $ext && $ext != $url))) {
            $ext = 'rar';
        }
        if($ext != 'rar' && $ext != 'txt' && $ext != 'zip') {
            echo2($basename . ' ' . $ext . ' ' . $url); 
            continue;
        }
        $savename_old = $contents['title']. '.'.$ext;
        $savename = str_ireplace(['(',')',':','*','?','/'], '', $savename_old);
        $savename = 'download/同步盘/qisuu_download/' . $contents['classname'] . '/' . $contents['typename'] . '/' . $savename;

        if(file_exists($savename) && filesize($savename) < 100) {
            unlink($savename);
        }
       
        if(file_exists($savename)) {
            $contents['savename'] = $savename;
            $redis->lPush('QISUU_POST_LINK', json_encode($contents));
            $total++;
        }
    }

    echo2($total . ' post queue');
}
*/

$maps = [
    'classname' => '小说栏目',
    'typename' => '小说类型',
    'title' => '小说名称',
    'info' => '小说信息',
    'description' => '描述',
    'download' => '下载地址1',
    'savename' => '下载地址2',
];

$typeids = [
    '玄幻奇幻' => 1,
    '武侠仙侠' => 2,
    '女频言情' => 3,
    '现代都市' => 4,
    '历史军事' => 5,
    '游戏竞技' => 6,
    '科幻灵异' => 7,
    '美文同人' => 8,
    '剧本教程' => 9,
    '名著杂志' => 10,
    '合集资源' => 11,
    '耽美纯爱' => 12,
];

$users = file('./qisuu_all/lnsuu_user.txt');

//从队列拿数据发布论坛文章
while (true) {
    $stop = $redis->get('QISUU_QUEUE_STOP');
    if($stop) {
        echo2('stop', 'QISUU_POST'); break;
    }

    $data = $redis->lPop('QISUU_POST_LINK');

    if(!$data) {
        echo2('empty', 'QISUU_POST'); break;
    }

    $author = '';
    $data = json_decode($data, true);
    $content = '[table=100%]';
    foreach ($data as $k=>$item) {
        if(is_array($item)) {
            if($k == 'info') {
                unset($item[0], $item[1], $item[4]);
                $author = $item[6];
                $item = implode("\n", $item);
            }
            elseif ($k == 'download') {
                $itemTemp = '[hide]';
                foreach ($item as $value) {
                    $itemTemp .= "[url={$value}]{$value}[/url]\n";
                }
                $itemTemp .= '[/hide]';

                $item = $itemTemp;
            }
        }

        if($k == 'description') {
            $item = str_ireplace(['<br/>','</p>','<p>'], "\n", $item);
        }
        elseif ($k == 'savename') {
            $item = "http://lnpan.b0.upaiyun.com/forum/books/" . str_ireplace('download/同步盘/qisuu_download/', '', $item);
            $item = "[hide][url={$item}]{$item}[/url][/hide]";
        }

        $kk = isset($maps[$k]) ? $maps[$k] : $k;
        $content .= "[tr][td=80]".$kk."[/td][td]".$item."[/td][/tr]";
    }
    $content .= '[/table]';

    $title = $data['title'];
    $typeid = $typeids[$data['classname']];

    shuffle($users);
    $user = $users[0];
    $user = preg_split('/\s+/', $user);
    $username = $user[0];
    $password = $user[1];
    $tags = $data['typename'];
    if($author) {
        $tags .= ','.$author;
    }

    $shell = "LC_ALL=en_US.UTF-8 /usr/bin/casperjs lnsuu_post.js '$title' '$content' '$typeid' '$username' '$password' '$tags'";
    //echo2($shell);
    $result = shell_exec($shell);
    echo2($result, 'QISUU_POST');
    //break;
}

sleep(600);

echo2('sleep exit', 'QISUU_POST');